//
//  PasswordView.m
//  SP2P
//
//  Created by 吴建东 on 16/6/21.
//  Copyright © 2016年 EIMS. All rights reserved.
//

#import "PasswordView.h"
#import "VerifyMobileViewController.h"

@interface PasswordView () {
    UIView      *_verifyPwdSuccessView;
    UIView      *_inputErrorView;
    UILabel     *_errorNumLabel;
    NSInteger   _errorNum;
}
@property (nonatomic, strong) NSArray        *titleArray;
@property (nonatomic, assign) BOOL           isSecurity;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, copy  ) NSString       *pwdStr;
@property (nonatomic, strong) UIView         *backView;
@property (nonatomic, strong) UIView         *keyBoardView;
@property (nonatomic, strong) UIView         *enterView;
@property (nonatomic, strong) UIButton       *cancelButton;
@property (nonatomic, strong) UILabel        *operationNameLabel;
@property (nonatomic, strong) UITextField    *passwordTextField;

@end

@implementation PasswordView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _originY = self.frame.origin.y;
        
        [self initData];
        [self loadInputView];
    }
    return self;
}

- (void)show {
    [self showInputView];
}

- (void)hidden {
    
}

#pragma mark - Data
- (void)initData {
    _dataArr = [NSMutableArray array];
    _isSecurity = YES;
    _pwdStr = @"";
    _titleArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"L",@"0",@"X"];
    
}

#pragma mark - Action
- (void)cancelButtonClick {
    [self hiddenInputView];
    
}
- (void)numButtomClick:(UIButton *)button {
    NSInteger index = button.tag - 100;
    if (index == 11) {
        if (_dataArr.count > 0) {
            [_dataArr removeLastObject];
        }
    }else if (index == 9) {
        
    }else {
        if (_dataArr.count < 6) {
            [_dataArr addObject:_titleArray[index]];
        }
        if (_dataArr.count >= 6) {
            [self inputSuccess];
        }
    }
    [self reloadEnterView];
}

- (void)inputSuccess {
    
    _pwdStr = [_dataArr componentsJoinedByString:@""];
//    [self verifyTradePasswordRequest];
}

- (void)reloadEnterView {
    for (UIView *subView in _enterView.subviews) {
        if ([subView isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)subView;
            textField.text = @"";
        }
    }
    if (_dataArr.count > 0) {
        for (NSInteger i=0; i < _dataArr.count; i++) {
            UITextField *textField = [_enterView viewWithTag:200 + i];
            if (_isSecurity) {
                textField.text = @"●";
            }else {
                textField.text = _dataArr[i];
            }
        }
    }
}

- (void)showInputView {
    [UIView animateWithDuration:0.3 animations:^{
        self.hidden = NO;

//        self.frame = RECTMAKE(0, _originY, self.frame.size.width, self.frame.size.height);
        
    } completion:^(BOOL finished) {
        
        
    }];
}

- (void)hiddenInputView {
    [UIView animateWithDuration:0.3 animations:^{
        self.hidden = YES;
        //self.frame = RECTMAKE(0, MSHIGHT, self.frame.size.width, self.frame.size.height);
        
    } completion:^(BOOL finished) {
        
        [_dataArr removeAllObjects];
        [self reloadEnterView];
        
        
    }];
}

- (void)inputButtonClick {
    
    [_dataArr removeAllObjects];
    [self reloadEnterView];
    
    [self inputErrorViewHidden];
}

- (void)forgetPwdButtonClick {
    if (_superViewController) {
        self.hidden = YES;
        _inputErrorView.hidden = YES;
        _keyBoardView.hidden = NO;
        
        [_dataArr removeAllObjects];
        [self reloadEnterView];
        
        //先验证手机号码
        VerifyMobileViewController *verifyMobile = [[VerifyMobileViewController alloc] init];
        verifyMobile.viewType = ResetDealPassword;
        verifyMobile.naviTitle = @"验证手机号";
        verifyMobile.mobileStr = AppDelegateInstance.userInfo.mobile;
        [_superViewController.navigationController pushViewController:verifyMobile animated:YES];
    }
}

- (void)reloadErrorNum:(NSInteger)errorNum {
    //_errorNumLabel.text = [NSString stringWithFormat:@"交易密码不正确，您还可以输入%ld次",3-errorNum];
    _errorNumLabel.text = [NSString stringWithFormat:@"原交易密码不正确!"];
    [self inputErrorViewShow];
}

- (void)inputErrorViewShow {
    //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"交易密码不正确" delegate:self cancelButtonTitle:@"重新输入" otherButtonTitles:@"找回密码", nil];
    //    [alert show];
    //    return;
    _keyBoardView.hidden = YES;
    _inputErrorView.hidden = NO;
    [AppDelegateInstance.window bringSubviewToFront:_inputErrorView];
}

- (void)inputErrorViewHidden {
    _keyBoardView.hidden = NO;
    _inputErrorView.hidden = YES;
    
}


#pragma mark - View
- (void)loadInputView {
    self.frame = CGRectMake(0, _originY, self.frame.size.width, self.frame.size.height);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    [AppDelegateInstance.window addSubview:self];
    self.hidden = YES;
    
    _backView = [UIView viewWithFrame:RECTMAKE(0, self.frame.size.height - MSWIDTH, MSWIDTH, MSWIDTH) backgroundColor:[UIColor whiteColor] superview:self];
    
    _cancelButton = [UIButton buttonWithFrame:RECTMAKE(0, 0, KAdaptNum(40), KAdaptNum(40)) backgroundColor:nil title:@"" titleColor:nil titleFont:0 target:self action:@selector(cancelButtonClick) superview:_backView];
    [_cancelButton setImage:[UIImage imageNamed:@"SetPayPwd_Cancel"] forState:UIControlStateNormal];
    
    _operationNameLabel = [UILabel labelWithFrame:RECTMAKE(10, 0, MSWIDTH - 20, KAdaptNum(40)) text:@"输入交易密码" textColor:KBlackColor textFont:16 fitWidth:NO superview:_backView];
    _operationNameLabel.textAlignment = NSTextAlignmentCenter;
    
    [UIView viewWithFrame:RECTMAKE(0, KAdaptNum(40)-1, MSWIDTH, 1) backgroundColor:KlayerBorder superview:_backView];
    
    _keyBoardView = [UIView viewWithFrame:RECTMAKE(0, KAdaptNum(40), MSWIDTH, GetHeight(_backView) - KAdaptNum(40)) backgroundColor:[UIColor whiteColor] superview:_backView];
    
    
    _enterView = [UIView viewWithFrame:RECTMAKE(10, KAdaptNum(20), MSWIDTH - 20, KAdaptNum(40)) backgroundColor:[UIColor whiteColor] superview:_keyBoardView];
    _enterView.layer.cornerRadius = 3.0f;
    _enterView.layer.borderColor = KlayerBorder.CGColor;
    _enterView.layer.borderWidth = 1.0f;
    
    for (NSInteger i=0; i < 6; i++) {
        UITextField *textField = [UITextField textFieldWithFrame:RECTMAKE((GetWidth(_enterView)/6)*i, 0, GetWidth(_enterView)/6, GetHeight(_enterView)) delegate:self text:@"" textColor:KBlackColor textFont:16 placeholder:@"" superview:_enterView];
        textField.userInteractionEnabled = NO;
        textField.tag = 200 + i;
        textField.textAlignment = NSTextAlignmentCenter;
        
        if (i < 5) {
            [UIView viewWithFrame:RECTMAKE(((GetWidth(_enterView))/6)*(i+1), 0, 1, GetHeight(_enterView)) backgroundColor:KlayerBorder superview:_enterView];
        }
    }
    
    for (NSInteger i=0; i < _titleArray.count; i++) {
        double w = MSWIDTH/3;
        double h = KAdaptNum(50);
        double x = w * (i%3);
        double y = GetMaxY(_enterView) + KAdaptNum(20) + h * (i/3);
        
        UIButton *button = [UIButton buttonWithFrame:RECTMAKE(x, y, w, h) backgroundColor:nil title:_titleArray[i] titleColor:KBlackColor titleFont:16 target:self action:@selector(numButtomClick:) superview:_keyBoardView];
        button.titleLabel.font = KFontBoldSize(16);
        button.tag = 100 + i;
        if (i == 9) {
            [button setTitle:@"" forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"ModifyDealPwd_Logo"] forState:UIControlStateNormal];
        }else if (i == 11) {
            [button setTitle:@"" forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"ModifyDealPwd_Delete"] forState:UIControlStateNormal];
        }
    }
    
    for (NSInteger i=0; i < 5; i++) {
        [UIView viewWithFrame:RECTMAKE(0, GetMaxY(_enterView) + KAdaptNum(20) + KAdaptNum(50) * i, MSWIDTH, 1) backgroundColor:KlayerBorder superview:_keyBoardView];
    }
    for (NSInteger i=0; i < 4; i++) {
        [UIView viewWithFrame:RECTMAKE(MSWIDTH/3*(i+1), GetMaxY(_enterView) + KAdaptNum(20), 1, KAdaptNum(50)*4) backgroundColor:KlayerBorder superview:_keyBoardView];
    }
}

- (void)loadVerifyPasswordSuccessView {
    _verifyPwdSuccessView = [UIView viewWithFrame:RECTMAKE(0, KAdaptNum(40), MSWIDTH, GetHeight(_backView)-KAdaptNum(40)) backgroundColor:[UIColor whiteColor] superview:_backView];
    
    UIImageView *successImageView = [UIImageView imageViewWithFrame:RECTMAKE((MSWIDTH - 50)/2, 60, 50, 50) imageFile:@"VerifyPwdSuccess" superview:_verifyPwdSuccessView];
    
    UILabel *label = [UILabel labelWithFrame:RECTMAKE(10, GetMaxY(successImageView) + 10, MSWIDTH - 20, KAdaptNum(20)) text:@"验证成功" textColor:GreenColor textFont:14 fitWidth:YES superview:_verifyPwdSuccessView];
    label.textAlignment = NSTextAlignmentCenter;
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_verifyPwdSuccessView removeFromSuperview];
//        [self cancelButtonClick];
//        [self tenderRequest];
    });
    
}

- (void)loadInputErrorView {
    
    _inputErrorView = [UIView viewWithFrame:RECTMAKE(0, 0, MSWIDTH, MSHIGHT) backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.3] superview:AppDelegateInstance.window];
    _inputErrorView.hidden = YES;
    
    UIView *errorBackView = [UIView viewWithFrame:RECTMAKE(35, (MSHIGHT - KAdaptNum(320)) + (KAdaptNum(320) - KAdaptNum(120))/2, MSWIDTH - 70, KAdaptNum(120)) backgroundColor:[UIColor whiteColor] superview:_inputErrorView];
    errorBackView.layer.cornerRadius = KAdaptNum(5.0);
    
    _errorNumLabel = [UILabel labelWithFrame:RECTMAKE(20, 0, GetWidth(errorBackView) - 40, KAdaptNum(120) - KAdaptNum(36) - 20) text:[NSString stringWithFormat:@"原交易密码不正确"] textColor:KBlackColor textFont:14 fitWidth:NO superview:errorBackView];
    _errorNumLabel.textAlignment = NSTextAlignmentCenter;
    _errorNumLabel.numberOfLines = 0;
    
    [UIView viewWithFrame:RECTMAKE(0, GetHeight(errorBackView) - KAdaptNum(40) - 1, GetWidth(errorBackView), 1) backgroundColor:KlayerBorder superview:errorBackView];
    
    [UIView viewWithFrame:RECTMAKE(GetWidth(errorBackView)/2, GetHeight(errorBackView) - KAdaptNum(40) - 1, 1, KAdaptNum(40)) backgroundColor:KlayerBorder superview:errorBackView];
    
    UIButton *inputButton = [UIButton buttonWithFrame:RECTMAKE(20, GetHeight(errorBackView) - KAdaptNum(40), (GetWidth(errorBackView)-60)/2, KAdaptNum(40)) backgroundColor:nil title:@"重新输入" titleColor:KBlackColor titleFont:14 target:self action:@selector(inputButtonClick) superview:errorBackView];
    
    UIButton *forgetButton = [UIButton buttonWithFrame:RECTMAKE(GetMaxX(inputButton) + 20, GetMinY(inputButton), (GetWidth(errorBackView)-60)/2, GetHeight(inputButton)) backgroundColor:nil title:@"找回密码" titleColor:BluewordColor titleFont:14 target:self action:@selector(forgetPwdButtonClick) superview:errorBackView];
    forgetButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
